package com.naicheng.ivote.adapter;

import java.util.ArrayList;

import com.naicheng.ivote.R;
import com.naicheng.ivote.doman.HotDetailButsetBean;
import com.naicheng.ivote.doman.TitleSelectlistBean;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

public class HotDetailButsetAdapter extends BaseAdapter {

	private ArrayList<HotDetailButsetBean> list = new ArrayList<HotDetailButsetBean>();
	private Activity activity = null;
	private Handler handler;

	/**
	 * 自定义构造方法
	 * 
	 * @param activity
	 * @param handler
	 * @param list
	 */
	public HotDetailButsetAdapter(Activity activity, Handler handler,
			ArrayList<HotDetailButsetBean> list) {
		this.activity = activity;
		this.handler = handler;
		this.list = list;
	}

	@Override
	public int getCount() {
		return list.size();
	}

	@Override
	public Object getItem(int position) {
		return position;
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		ViewHolder holder = null;
		if (convertView == null) {
			holder = new ViewHolder();
			convertView = LayoutInflater.from(activity).inflate(R.layout.listselect_main_item, null);
			holder.textView = (TextView) convertView.findViewById(R.id.listselect_main_item_content);
			holder.textViewline = (TextView) convertView.findViewById(R.id.listselect_main_item_line);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		
		holder.textView.setText(list.get(position).getText());
		holder.textView.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Message msg = new Message();
				Bundle data = new Bundle();
				// 设置选中索引
				data.putInt("selIndex", list.get(position).getId());
				msg.setData(data);
				msg.what = list.get(position).getId();
				// 发出消息
				handler.sendMessage(msg);
				
			}
		});
		return convertView;
	}

	static class ViewHolder {
		TextView textView;
		TextView textViewline;
	}

}
